package cn.imvc.app.controllers.goods;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import cn.imvc.app.commons.SystemConfig;
import cn.imvc.app.entities.Goods;
import cn.imvc.app.entities.GoodsPrice;
import cn.imvc.app.entities.GoodsReviews;
import cn.imvc.app.entities.GoodsSku;
import cn.imvc.app.entities.User;
import cn.imvc.app.models.GoodsModel;
import cn.imvc.app.models.GoodsPriceModel;
import cn.imvc.app.models.GoodsReviewsModel;
import cn.imvc.app.models.GoodsSkuModel;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.app.models.UserModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

public class GoodsReviewsControllerImpl implements GoodsReviewsController {
	static Logger logger = Logger.getLogger(GoodsReviews.class);
	
	@Autowired
	GoodsReviewsModel goodsReviewsModel;
	@Autowired
	GoodsModel goodsModel;
	@Autowired
	UserModel userModel;
	@Autowired
	GoodsSkuModel goodsSkuModel;
	@Autowired
	public SystemConfig systemConfig;
	@Autowired
	GoodsPriceModel goodsPriceModel;
	@Autowired
	LoggingModel loggingModel;
	
//	public Page<GoodsReviews> page;
	public Page<Object[]> page;
	public ViewRequest viewRequest;
	public GoodsReviews goodsReviews;
	public int id;
	public int[] ids;
	public int goodsId;
	public User user;
	public String userName;
	public String goodsName;
	public List<GoodsSku> goodsSkus;
	public List<GoodsPrice> goodsPrices;
	public int goodsSkuSize;
	
	public void setGoodsName(String goodsName) {
		this.goodsName = goodsName;
	}

	public void setGoodsReviews(GoodsReviews goodsReviews) {
		this.goodsReviews = goodsReviews;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public void setPage(Page<Object[]> page) {
		this.page = page;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public void setGoodsId(int goodsId) {
		this.goodsId = goodsId;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String search() {
		// TODO Auto-generated method stub
		return null;
	}

	public String index() {
//		page=goodsReviewsModel.find(viewRequest);
		page=goodsReviewsModel.findObjectPage(viewRequest);
		return "goods-reviews/index";
	}

	public String show() {
		// TODO Auto-generated method stub
		return null;
	}

	public String editNew() {
		// TODO Auto-generated method stub
		return null;
	}

	//不需要手动创建
	public String create() {
//		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//		if (principal instanceof UserDetails) {
//		   userName = ((UserDetails)principal).getUsername();
//		} else {
//		   userName = principal.toString();
//		}
//		User user = userModel.get(userName);
//		goodsReviews.setUserId(user.getId());
//		goodsReviews.setCreatedAt(new Date());
//		goodsReviews.setUpdatedAt(new Date());
//		goodsReviewsModel.save(goodsReviews);
//		Goods goods = goodsModel.get(goodsReviews.getGoods().getId());
//		goods.setVerify(goodsReviews.getState());
//		goods.setVerifyRemark(goodsReviews.getRemark());
//		goodsModel.update(goods);
//		return Render.json(new DialogBean("200", "审批完成","",""));
		return null;
	}

	public String edit() {
		goodsReviews = goodsReviewsModel.get(id);
		Goods goods = goodsReviews.getGoods();
		goodsSkuSize = goods.getGoodsSku().size();
		//sku无法实现price排序，废弃
//		goodsSkus = goodsSkuModel.findByGoods(goodsReviews.getGoods());
		goodsPrices = goodsPriceModel.getGoodsPrices(goods.getId());
		goodsSkuSize = goodsPrices.size()/goodsSkuSize;
		User user = userModel.getSessionUser();
		userName = user.getName();
		return "goods-reviews/edit";
	}

	public String update() {
		goodsReviewsModel.updateReviews(goodsReviews);
		return Render.json(new DialogBean("200", "审批完成","",""));
	}

	public String destroy() {
		// TODO Auto-generated method stub
		return null;
	}

	public String destroyBatch() {
		// TODO Auto-generated method stub
		return null;
	}

}
